package com.wcs.exam.controller.biz;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wcs.exam.common.enums.MenuTypeEnum;
import com.wcs.exam.common.enums.StatusEnum;
import com.wcs.exam.common.util.BeanUtil;
import com.wcs.exam.controller.admin.resp.AdminSysMenuUserResp;
import com.wcs.exam.controller.biz.resp.LoginRouterResp;
import com.wcs.exam.dao.SysMenuDao;
import com.wcs.exam.dao.SysMenuRoleDao;
import com.wcs.exam.dao.SysRoleUserDao;
import com.wcs.exam.dao.impl.mapper.entity.SysMenu;
import com.wcs.exam.dao.impl.mapper.entity.SysMenuRole;
import com.wcs.exam.dao.impl.mapper.entity.SysRoleUser;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wcs
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SysUserCommonBiz {

    @NotNull
    private final SysRoleUserDao sysRoleUserDao;
    @NotNull
    private final SysMenuRoleDao sysMenuRoleDao;
    @NotNull
    private final SysMenuDao sysMenuDao;

    public List<SysMenu> listMenu(Long userId) {
        List<SysRoleUser> roleUsers = sysRoleUserDao.listByUserIds(Collections.singletonList(userId));
        if (CollectionUtil.isEmpty(roleUsers)) {
            return new ArrayList<>();
        }
        List<SysMenuRole> menuRoles = sysMenuRoleDao.listByRoleIds(roleUsers.stream().map(SysRoleUser::getRoleId).collect(Collectors.toList()));
        if (CollectionUtil.isEmpty(menuRoles)) {
            return new ArrayList<>();
        }

        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getStatus, StatusEnum.NORMAL.getCode());
        wrapper.in(SysMenu::getId, menuRoles.stream().map(SysMenuRole::getMenuId).collect(Collectors.toList()));
        wrapper.orderByAsc(SysMenu::getSort).orderByDesc(SysMenu::getId);
        List<SysMenu> sysMenus = sysMenuDao.list(wrapper);
        if (CollectionUtil.isEmpty(sysMenus)) {
            return new ArrayList<>();
        }
        return sysMenus;
    }

    /**
     * 获取路由
     */
    public List<LoginRouterResp> routerList(List<SysMenu> menuList) {
        menuList = menuList.stream().filter(item -> item.getMenuType().equals(MenuTypeEnum.MENU.getCode())).collect(Collectors.toList());
        return BeanUtil.copyProperties(menuList, LoginRouterResp.class);
    }

    /**
     * 获取菜单
     */
    public List<AdminSysMenuUserResp> menuList(List<SysMenu> menuList) {
        return filters(0L, menuList);
    }

    /**
     * 菜单层级处理
     */
    private List<AdminSysMenuUserResp> filters(Long parentId, List<SysMenu> menuList) {
        List<SysMenu> sysMenuList = menuList.stream().filter(item -> parentId.compareTo(item.getParentId()) == 0 && !item.getMenuType().equals(MenuTypeEnum.PERMISSION.getCode())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(sysMenuList)) {
            List<AdminSysMenuUserResp> respList = BeanUtil.copyProperties(sysMenuList, AdminSysMenuUserResp.class);
            for (AdminSysMenuUserResp resp : respList) {
                resp.setChildren(filters(resp.getId(), menuList));
            }
            return respList;
        }
        return null;
    }

}
